<?php

/**
 * gift actions.
 *
 * @package    sf_sandbox
 * @subpackage gift
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 12479 2008-10-31 10:54:40Z fabien $
 */
class giftActions extends sfActions
{
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
	
	
  public function executeAdd(sfWebRequest $request)
  {
  	$str = 'Type11Form';
    $this->form = new $str();
    
  	 if ($request->isMethod('post'))

    {

      $this->form->bind($request->getParameter('material'),$request->getFiles('material'));
      if ($this->form->isValid())
      {
      	
        $this->form->save();
        if($this->form->getObject()->getParentId()>0)
        {
          $this->setRedirect($this->form->getObject()->getTypeId(),$this->form->getObject()->getParentId());
        }
        else 
        {
         $this->redirect('gift/index');
        }
      }

    }
    
  }
  
	
	
  public function executeIndex(sfWebRequest $request)
  {
       ////Удаляем
  	    if($request->getParameter('del')==1)
  	    {
  	        $m = MaterialPeer::retrieveByPK($request->getParameter('id'));
		  	if($this->getUser()->isSuperAdmin() and $m)
		  	{
		  		$m->delete();
		  	}
  	    }
  	
  	$c = new Criteria();
  	if($request->getParameter('folder_id'))
  	{
  			$c->addJoin(Mat2FolderPeer::MATERIAL_ID,MaterialPeer::ID);
  			$c->add(Mat2FolderPeer::FOLDER_ID,$request->getParameter('folder_id'));
  	}
  
  	
  	$c->add(MaterialPeer::TYPE_ID,11);
  	$c->addDescendingOrderByColumn(MaterialPeer::ID);
  	
  			   				$pager = new sfPropelPager ( 'Material', 10 );
							$pager->setCriteria ( $c );
							$pager->setPeerMethod ( 'doSelect' );
							$pager->setPage ( $this->page );
							$pager->init ();
							$this->gifts = $pager;
							
							
  }
	
	
  public function executeCategory(sfWebRequest $request)
  {

        ////Проверка на заполненность профайла
  	if(!$this->getUser()->getProfile()->getFullProfile())
  	{
  	 $this->getUser ()->setFlash ( 'error', __('Вы не полностью заполнили профайл') );
  	 $this->redirect('profile/step2');
  	}
        ////

  	
  	if($request->getParameter('id_to'))
  	{
  		$_SESSION['to_id_gift'] = $request->getParameter('id_to');
  		
  	}
  	
  			sfLoader::loadHelpers ( 'Tag' );
		sfLoader::loadHelpers ( 'Url' );
  	if(!$request->getParameter('id'))
  	{
  		$this->folder = FolderPeer::retrieveByPK(sfConfig::get('app_folder_gift'));
  	}
  	else
  	{
  		$this->folder = FolderPeer::retrieveByPK($request->getParameter('id'));
  	}
  	$this->root_folder = FolderPeer::retrieveByPK(sfConfig::get('app_folder_gift'));
  	$this->forward404Unless($this->folder);
  	
  	////Добавление подарка в корзину
  	if($request->getParameter('add_to_box'))
  	{
  		$link = link_to(__('корзину'),'@giftbox_output');
  		if($this->getUser()->isAuthenticated())
  		{
  			$g = new Giftbox();
  			$g->setFromId($this->getUser()->getAttribute( 'user_id', null, 'sfGuardSecurityUser' ));
  			$g->setGiftId($request->getParameter('add_to_box'));
  			if($_SESSION['to_id_gift']>0)
  			{
  				$g->setToId($_SESSION['to_id_gift']);
  			}
  			$g->setPub(false);
  			$g->save();
  			
  		        $this->getUser ()->setFlash ( 'message', __('Подарок добавлен'));
                        $this->redirect('giftbox/output');
  		}
  		else 
  		{
  		 $this->getUser ()->setFlash ( 'error', __('Только для зарегистрированных пользователей') );
                 $this->redirect('@registration');
  		}
  	}
  	///
  	
  }
  
  
  public function executeCategory_add(sfWebRequest $request)
  {
    $this->form = new Type11Form();
    
      if ($request->isMethod('post'))

    {
 
      $this->form->bind($request->getParameter('giftcategory'),$request->getFiles('giftcategory'));
      if ($this->form->isValid())
      {
      	  
         $this->form->save();
         $this->redirect('@gift_category');
         }

    }
    
  }
  
  
  
  public function executeItem_add(sfWebRequest $request)
  {
    $this->form = new GiftForm();
    
      if ($request->isMethod('post'))

    {
 
      $this->form->bind($request->getParameter('gift'),$request->getFiles('gift'));
      if ($this->form->isValid())
      {
      	  
         $this->form->save();
         $this->redirect('@gift_gallery?id='.$this->form->getObject()->getCategoryId());
         }

    }
    
  }
  
  
   public function executeGallery(sfWebRequest $request)
  {

    $this->forward404Unless($request->getParameter('id'));
    $c = new Criteria();
    $c->add(GiftPeer::CATEGORY_ID,$request->getParameter('id'));
    $c->addDescendingOrderByColumn ( GiftPeer::ID );
    
    	$pager = new sfPropelPager('Gift', sfConfig::get('app_giftcategory_perpage'));
        $pager->setCriteria($c);
        $pager->setPeerMethod('doSelectWithI18n');        
        $pager->setPage($this->page);
        $pager->init();
        $this->gift = $pager;
  }
  
  
   public function executeShow(sfWebRequest $request)
  {
  	$this->forward404Unless($request->getParameter('id'));
    $this->gift = GiftPeer::retrieveByPK($request->getParameter('id'));
    $this->forward404Unless($this->gift);
  }
  
  
   public function executeSend(sfWebRequest $request)
  {
  	$this->form = new GiftboxForm();
	
	if($request->getParameter('id'))
	{
		$gift = sfGuardUserPeer::retrieveByPK($request->getParameter('id'));
		if($gift)
		$this->form->setDefault('gift', $gift->getId());
	}
	
	 if ($request->isMethod('post'))

    {

      $this->form->bind($request->getParameter('mailbox'),$request->getFiles('mailbox'));
      if ($this->form->isValid())
      {
      	$to_user = sfGuardUserPeer::retrieveByUsername($request->getParameter('mailbox[to_user]'));
      	if($to_user)
        $this->form->save();
        $this->form->getObject()->setToId($to_user->getId());
        $this->form->getObject()->save();
        
        $this->redirect('@mailbox_output');
      }

    }
  }
  
  
  public function executeEdit(sfWebRequest $request)
  {
  	
     $this->mat = MaterialPeer::retrieveByPK($request->getParameter('id'));
     $this->form = new Type11Form($this->mat);
     
        if ($request->isMethod('post'))

    {
          $this->mat = MaterialPeer::retrieveByPK($request->getParameter('material[id]'));
          $this->form = new Type11Form($this->mat);
      $this->form->bind($request->getParameter('material'),$request->getFiles('material'));
      if ($this->form->isValid())
      {
      	  
         $this->form->save();
         $this->redirect('gift/index');
         }

    }
     
  }
  
  
}
